package com.mk.plugin.function;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SearchInputFunction extends Functions {

    public SearchInputFunction(String fieldName) {
        super(fieldName.equals("") ? "searchKey": fieldName);
    }

    @Override
    public List<String> getSubviewProps() {
        return Arrays.asList(
                "tempSearchKey",
                "onConfirmSearchKey",
                "onChangeSearchKey"
        );
    }

    @Override
    public List<String> getNeedComponent() {
        return Arrays.asList(
                "MKToolbar",
                "MKToolbarLeftAction"
        );
    }

    @Override
    public List<String> getNeedMapToProps() {
        return Arrays.asList("tempSearchKey");
    }

    @Override
    public String getLeftActionComponentString() {
        return "<MKSearchInput\n" +
                "                            searchInputValue={tempSearchKey}\n" +
                "                            searchInputOnClick={onConfirmSearchKey}\n" +
                "                            searchInputOnChange={onChangeSearchKey}\n" +
                "                            searchInputDefaultText=\"请输入券名称\"\n" +
                "                        />";
    }

    @Override
    public String getActionString(Map<String, Object> properties) {
        return "export function onChangeSearchKey(event) {\n" +
                "    return {\n" +
                "        type : ActionType.ON_CHANGE_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + ",\n" +
                "        key  : event.target.value\n" +
                "    }\n" +
                "}\n" +
                "\n" +
                "export function onConfirmSearchKey() {\n" +
                "    return {\n" +
                "        type : ActionType.ON_CONFIRM_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + "\n" +
                "    }\n" +
                "}\n" +
                "\n";
    }

    @Override
    public String getActionTypeString(Map<String, Object> properties) {
        return "export const ON_CHANGE_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + " = \"ON_CHANGE_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + "\";\n" +
                "\n" +
                "export const ON_CONFIRM_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + " = \"ON_CONFIRM_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + "\";\n" +
                "\n";
    }

    @Override
    public String getReducerSwitches(Map<String, Object> properties) {
        return "case ActionType.ON_CHANGE_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + ":\n" +
                "            return handleOnChangeSearchKeyFor" + properties.get("UpperPageName") + "(state, action);\n" +
                "\n" +
                "        case ActionType.ON_CONFIRM_SEARCH_KEY_FOR_" + properties.get("allUpperCaseUnderscoreName") + ":\n" +
                "            return handleOnConfirmSearchKeyFor" + properties.get("UpperPageName") + "(state);\n" +
                "\n";
    }

    @Override
    public String getReducerFunctions(Map<String, Object> properties) {
        return "function handleOnChangeSearchKeyFor" + properties.get("UpperPageName") + "(state, action) {\n" +
                "    return Object.assign({}, state, {\n" +
                "        tempSearchKey : action.key\n" +
                "    })\n" +
                "}\n" +
                "\n" +
                "function handleOnConfirmSearchKeyFor" + properties.get("UpperPageName") + "(state) {\n" +
                "    let searchCondition = Object.assign({}, state.searchCondition, {\n" +
                "        " + this.getFieldName() + " : state.tempSearchKey\n" +
                "    });\n" +
                "\n" +
                "    return Object.assign({}, state, {\n" +
                "        searchCondition\n" +
                "    })\n" +
                "}\n" +
                "\n";
    }

    @Override
    public List<String> getSearchConditionInitState(Map<String, Object> properties) {
        return Arrays.asList(
                this.getFieldName() + ": ''"
        );
    }

    @Override
    public List<String> getReducerInitState(Map<String, Object> properties) {
        return Arrays.asList("tempSearchKey   : \"\"");
    }

    @Override
    public List<String> getMKComponentImport() {
        return Arrays.asList("MKSearchInput");
    }
}
